import fs from 'fs';
import * as readline from "readline";
import {HtmlTemplate, indexTemplate} from "./template.js";
const read = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
const createFile = (dirTitle, path) => {
    fs.writeFileSync(`./src/views${path}/${dirTitle}/index.jsx`, indexTemplate(dirTitle));
    fs.writeFileSync(`./src/views${path}/${dirTitle}/html.jsx`, HtmlTemplate(dirTitle));
    fs.writeFileSync(`./src/views${path}/${dirTitle}/index.less`, '');
    import('./updateRouter.js');
    console.log('组件创建成功，文件路径为：',path + '/' + dirTitle.slice(0,1).toUpperCase() +dirTitle.slice(1).toLowerCase());
    console.log(`\x1B[36m此组件路由为：${(path + '/' + dirTitle).toLowerCase()}\x1B[0m`);
    console.log('\x1B[31m请勿随意修改index.jsx下默认生成的注释\x1B[0m');
    read.close();
}
const createDir = (title, path) => {
    title = title.slice(0, 1).toUpperCase() + title.slice(1).toLowerCase()
    fs.mkdir(`./src/views${path}/${title}`, () => {
        createFile(title, path)
    });
}
inputName();
function inputName(){
    read.question('请输入组件名称：',(title)=>{
        if (!/^[a-zA-Z]+$/.test(title)) {
            console.log('\x1B[31m组件名称必须为英文\x1B[0m');
            inputName();
        }else {
            inputPath(title);
        }
    });
}
function inputPath(title){
    read.question('请输入组件所处路径：(例如：/Home，意思为在Home下创建此组件)', (path) => {
        if(!/^[a-zA-Z\/]+$/.test(path) && path[0] !== '/'){
            console.log('\x1B[31m路径开头必须为“/“且必须全为英文\x1B[0m');
            inputPath(title);
        }else{
            createDir(title, path)
        }
    });
}